package org.dtrd.modules.food.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.common.aspect.annotation.AutoLog;
import org.dtrd.modules.food.entity.bean.RecipeDetail;
import org.dtrd.modules.food.entity.bean.RecipeListInfo;
import org.dtrd.modules.food.entity.request.RecipePageRequest;
import org.dtrd.modules.food.service.IDtrdExtEmsYwRecipesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static org.dtrd.common.constant.CommonConstant.*;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_OPERATION_FAILURE;
import static org.dtrd.config.system.SystemState.ResponseState.STATE_PARAMS_MISSING;

/**
 * <p>
 * 食谱 前端控制器
 * </p>
 * @author QR
 * @since 2021-12-08
 */
@Api(tags = "食物库")
@RestController
@RequestMapping("/100/16/")
public class RecipesController extends BaseController {

    @Autowired
    private IDtrdExtEmsYwRecipesService recipesService;

    @ApiOperation(value = "查询食谱详情", response = RecipeDetail.class)
    @GetMapping("1001616")
    @AutoLog(value="查询食谱详情", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getRecipes(@RequestParam Integer id){
        RecipeDetail info = recipesService.getRecipes(id);
        return Result.OK(info);
    }

    @ApiOperation(value = "分页查询食谱列表", response = RecipeListInfo.class)
    @PostMapping("1001615")
    @AutoLog(value="查询食谱列表", operateType = OPERATE_TYPE_QUERY)
    public Result<?> getRecipesList(@RequestBody RecipePageRequest request){
        handlePageRequest(request);
        IPage<RecipeListInfo> page = recipesService.pageRecipe(request);
        return Result.OK(page);
    }

    @ApiOperation("新增或修改食谱")
    @PostMapping("1001617")
    @AutoLog(value="新增或修改食谱", operateType = OPERATE_TYPE_SAVE_OR_UPDATE)
    public Result<?> saveOrUpdateRecipes(@RequestBody RecipeDetail dto){
        if (StrUtil.isBlank(dto.getRecipeName())
                || dto.getRecipeCategoryid() == null) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = recipesService.saveOrUpdateRecipes(dto);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }

    @ApiOperation("删除食谱")
    @DeleteMapping("1001618")
    @AutoLog(value="删除食谱", operateType = OPERATE_TYPE_DELETE)
    public Result<?> removeRecipes(@RequestParam Integer id){
        if(isIntParamInvalid(id)) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean result = recipesService.removeRecipes(id);
        return result ? Result.OK() : error(STATE_OPERATION_FAILURE);
    }
}
